// JavaScript Document
$(function () {
    init()
    function init(){
        $("#theme").click()
        refreshPersonalInfo()
        refreshPersonnelMatter()
        refreshStaffAccount()
        refreshStaffRP()
        refreshRoad();
        refreshCLLsit();
        overTimeCheck()
    }
    function overTimeCheck(){
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/overTimeCheck",
            success:function(data){
                // alert(data.msg)
            },
            error:function () {
                alert("错误");
            }
        });
    }
    function refreshCLLsit() {
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/get_AllCLInfo",
            success:function(data){
                // alert(data.msg)
                var str=""
                $("#CLLsitTableInput").html(str)
                if(data.status==1){
                    $("#CLLsitTableInput").append("<tr><td colspan='9'>暂无记录</td></tr>")
                }else {
                    for(var i=0;i<data.data.length;i++) {
                        str = ' <tr >\n' +
                            '<td >'+data.data[i].clid+'</td>\n' +
                            '<td >'+data.data[i].uid+'</td>\n' +
                            '<td >'+data.data[i].name+'</td>\n' +
                            '<td>'+data.data[i].type+'</td>\n' +
                            '<td>'+data.data[i].start_time+'</td>\n' +
                            '<td>'+data.data[i].end_time+'</td>\n' +
                            '<td>'+data.data[i].detail+'</td>\n' +
                            '<td>'+data.data[i].signUp_time+'</td>\n' +
                            '<td>'+data.data[i].pass_time+'</td>\n'
                        switch (data.data[i].status) {
                            case 0: str+=  '<td class="status0"><b>未审批</b></td><td><a name="CLPass" class="btn pill fill">通过</a> <a name="CLRefuse" class="btn pill fill">拒绝</a></td>\n'
                                break;
                            case 1:str+=  '<td class="status1"><b>通过</b></td><td></td>\n'
                                break;
                            case 2:str+=  '<td class="status2"><b>未通过</b></td><td><a name="CLLsitDelete" class="btn pill fill">删除</a> </td>\n'
                                break;
                            case 3:str+=  '<td class="status3"><b>审批超时</b></td><td><a name="CLLsitDelete" class="btn pill fill">删除</a> </td>\n'
                                break;
                        }

                        str+=  '</tr>'
                        $("#CLLsitTableInput").append(str)
                    }
                }


            },

            error:function () {
                alert("错误");
            }
        });
    }
    $(document).on("click","a[name='CLLsitDelete']",function(){
      var clid= $($(this).parents('tr').children('td').get(0)).html()
        var r = confirm("确定删除这条记录吗？");
        if (r == true) {
            $.ajax({
                type:"post",
                data:{clid:clid},
                url:"http://127.0.0.1:8080/delete_StaffCL",
                success:function(data){
                    alert(data.msg)
                    init()
                },
                error:function () {
                    alert("错误");
                }
            });
        }
    })
    $(document).on("click","a[name='CLRefuse']",function(){
        var clid= $($(this).parents('tr').children('td').get(0)).html()
        $.ajax({
            type:"post",
            data:{clid:clid},
            url:"http://127.0.0.1:8080/refuseCL",
            success:function(data){
                alert(data.msg)
                init()
            },
            error:function () {
                alert("错误");
            }
        });
    })
    $(document).on("click","a[name='CLPass']",function(){
        var clid= $($(this).parents('tr').children('td').get(0)).html()
        $.ajax({
            type:"post",
            data:{clid:clid},
            url:"http://127.0.0.1:8080/passCL",
            success:function(data){
                alert(data.msg)
                init()
            },
            error:function () {
                alert("错误");
            }
        });
    })
    $("#logout").on("click",function () {
        formpost("http://127.0.0.1:8080/show_login",{});
    })
    $("#addStaffConfirm").on("click",function (){
        var uesrname=$("#usernameInput").val();
        var password=$("#passwordInput").val();
        var name=$("#newNameInput").val();
        var sex=$("input[name='newSex']:checked").val();
        var phone=$("#newPhoneInput").val();
        var address=$("#newAddressInput").val();
        var birthday=$("#newBirthInput").val();
        var department=$("#newDepartmentInput").val();
        var position=$("#newPositionInput").val();
        var salary=$("#newSalaryInput").val();
        var isRegular=$("input[name='newIsRegular']:checked").val();
        if(uesrname==""||password==""||name==""||sex==null||phone==""||address==""||birthday==""||department==""||position==""||salary==""||isRegular==null){
            alert("请填写完整信息！")
        }else {
            $.ajax({//验证username是否重复
                type:"post",
                data:{uesrname:uesrname,
                   },
                url:"http://127.0.0.1:8080/check_username",
                success:function(data){
                    //alert(data.msg)
                    if(data.status==1){
                       $.ajax({
                            type:"post",
                            data:{uesrname:uesrname,
                                password:password,
                                name:name,
                                sex:sex,
                                phone:phone,
                                address:address,
                                birthday:birthday,
                                department:department,
                                position:position,
                                salary:salary,
                                isRegular:isRegular},
                            url:"http://127.0.0.1:8080/add_staff",
                            success:function(data){
                                alert(data.msg)
                                init();
                            },

                            error:function () {
                                alert("错误");
                            }
                        });
                    }else {
                        alert("用户名已存在！")
                    }
                },

                error:function () {
                    alert("错误");
                }
            });

        }
    })
    $("#mainRefresh").on("click",function (){
        init();
        alert("已刷新")
    })
    $("#updatePersonalInfConfirm").on("click",function (){
      //  alert("确认")
       var piid=$("#piid").val();
       var name= $("#nameInput").val()
       var sex= $("input[name='sex']:checked").val();
       var phone=$("#phoneInput").val()
       var address= $("#addressInput").val()
       var birthday=  $("#birthInput").val()
        if(name==''||sex==null||phone==""||address==""||birthday==""){
            alert("请输入完整信息！")
        }else {
            $.ajax({
                type:"post",
                data:{piid:piid,name:name,sex:sex,phone:phone,address:address,birthday:birthday},
                url:"http://127.0.0.1:8080/update_personalInfo",
                success:function(data){
                    //alert("ajax成功")
                    alert(data.msg)
                    $(".box").attr("hidden",true)
                    $("#boxPersonalInfo").attr("hidden",false)
                    init();
                    /*$("#piid").val("")
                    $("#nameInput").val("")
                    $("#phoneInput").val("")
                    $("#addressInput").val("")
                    $("#birthInput").val("")*/
                },

                error:function () {
                    alert("错误");
                }
            });
        }
    })
    $("#updatePersonalInfCancel").on("click",function () {
       // alert("asdasda")
        $(".box").attr("hidden",true)
        $("#boxPersonalInfo").attr("hidden",false)
    })
    $(document).on("click","a[name='personalInfoUpdate']",function(){
       // alert("修改"+$(this).parents('tr').attr('id'));
        $("#piid").val($(this).parents('tr').attr('id'));
        $("#nameInput").attr("value",$($(this).parents('tr').children('td').get(1)).html());
        var sex=$($(this).parents('tr').children('td').get(2)).html()
        if(sex=="男"){
            $("#sexMan").attr("checked","true")
        }else {
            $("#sexWomen").attr("checked","true")
        }
        $("#phoneInput").attr("value",$($(this).parents('tr').children('td').get(3)).html());
        $("#addressInput").attr("value",$($(this).parents('tr').children('td').get(4)).html());
        $("#birthInput").attr("value",$($(this).parents('tr').children('td').get(5)).html());

        $(".box").attr("hidden",true)
        $("#boxUpdatePersonalInfo").attr("hidden",false)
       /* $("#boxUpdatePersonalInfo").show();
        $("#boxUpdatePersonalInfo").siblings().hide();*/
    })
/*    $(document).on("click","a[name='personalInfoDelete']",function(){
        var piid=$(this).parents('tr').attr('id')
        $.ajax({
            type:"post",
            data:{piid:piid},
            url:"http://127.0.0.1:8080/delete_personalInfo",
            success:function(data){
                //alert("ajax成功")
                alert(data.msg)
                refreshPersonalInfo()
            },

            error:function () {
                alert("错误");
            }
        });
    })*/

    function refreshPersonalInfo(){

        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/refreshPersonalInfo",
            success:function(data){
               // alert("ajax成功")
                var str=""
                $("#personalInfoTableInput").html(str)
                for(var i=0;i<data.data.length;i++){
                    str=' <tr id="'+data.data[i].piid+'">\n' +
                        '<td >'+data.data[i].uid+'</td>\n' +
                        '<td>'+data.data[i].name+'</td>\n' ;
                    var sex=data.data[i].sex
                    if(sex==1){
                        str+= '<td>男</td>\n'
                    }else {
                        str+= '<td>女</td>\n'
                    }
                    str+= '<td>'+data.data[i].phone+'</td>\n' +
                        '<td>'+data.data[i].address+'</td>\n'
                    var birth=data.data[i].birthday
                   // alert(birth)
                    str+='<td>'+birth.substring(0,10)+'</td>\n' +
                        '<td><a name="personalInfoUpdate" class="btn pill  fill">修改</a> </td>\n' +
                        '</tr>'
                    $("#personalInfoTableInput").append(str);
                }
            },

            error:function () {
                alert("错误");
            }
        });
    }
    function refreshPersonnelMatter(){

        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/refreshPersonenlMatter",
            success:function(data){
                //alert(data.msg)
                var str=""
                $("#personnelMatterTableInput").html(str)
                for(var i=0;i<data.data.length;i++){
                    str=' <tr id="'+data.data[i].pmid+'">\n' +
                        '<td>'+data.data[i].uid+'</td>\n' +
                        '<td>'+data.data[i].name+'</td>\n' +
                        '<td>'+data.data[i].join_time.substring(0,10)+'</td>\n' +
                        '<td>'+data.data[i].department+'</td>\n' +
                        '<td>'+data.data[i].position+'</td>\n' +
                        '<td>'+data.data[i].salary+'</td>\n'
                    var isRegular=data.data[i].is_regular
                    if(isRegular==1){
                        str+='<td>是</td>\n' +
                            '<td><a name="personnelMatterUpdate" class="btn pill fill">修改</a> <!--<a name="personalInfoDelete" class="btn pill fill">删除</a>--></td>\n' +
                            '</tr>'
                    }else {
                        str+='<td>否</td>\n' +
                            '<td><a name="personnelMatterUpdate" class="btn pill fill">修改</a> <!--<a name="personalInfoDelete" class="btn pill fill">删除</a>--></td>\n' +
                            '</tr>'
                    }

                    $("#personnelMatterTableInput").append(str);
                }
            },

            error:function () {
                alert("错误");
            }
        });
    }
    $(document).on("click","a[name='personnelMatterUpdate']",function(){
     //   alert("修改"+$(this).parents('tr').attr('id'));
        $("#pmid").val($(this).parents('tr').attr('id'));

        $("#joinInput").attr("value",$($(this).parents('tr').children('td').get(2)).html());
        $("#departmentInput").attr("value",$($(this).parents('tr').children('td').get(3)).html());
        $("#positionInput").attr("value",$($(this).parents('tr').children('td').get(4)).html());
        $("#salaryInput").attr("value",$($(this).parents('tr').children('td').get(5)).html());
        var is=$($(this).parents('tr').children('td').get(6)).html()
        if(is=="是"){
            $("#yes").attr("checked","true")
        }else {
            $("#no").attr("checked","true")
        }

        $(".box").attr("hidden",true)
        $("#boxUpdatePersonnelMatter").attr("hidden",false)
        /* $("#boxUpdatePersonalInfo").show();
         $("#boxUpdatePersonalInfo").siblings().hide();*/
    })
    $("#updatePersonnelMatterCancel").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxPersonnelMatter").attr("hidden",false)
    })
    $("#updatePersonnelMatterConfirm").on("click",function () {
        var pmid=$("#pmid").val();
        var joinTime=$("#joinInput").val();
        var departmen= $("#departmentInput").val();
        var position=$("#positionInput").val();
        var salary=$("#salaryInput").val();
        var isRegular= $("input[name='isRegular']:checked").val();
        if(joinTime==""||departmen==""||position==""||salary==""||isRegular==""){
            alert("请输入完整信息!")
        }else {
            $.ajax({
                type:"post",
                data:{pmid:pmid,joinTime:joinTime,departmen:departmen,position:position,salary:salary,isRegular:isRegular},
                url:"http://127.0.0.1:8080/update_personnelMatter",
                success:function(data){
                    //alert("ajax成功")
                    alert(data.msg)
                    $(".box").attr("hidden",true)
                    $("#boxPersonnelMatter").attr("hidden",false)
                    init();
                    /*$("#pmid").val("")
                    $("#nameInput1").val("")
                    $("#joinInput").val("")
                    $("#departmentInput").val("")
                    $("#positionInput").val("")
                    $("#salaryInput").val("")*/
                },

                error:function () {
                    alert("错误");
                }
            });
        }
    })
    function refreshStaffAccount(){
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/refreshStaffAccount",
            success:function(data){
                var str=""
                $("#staffAccountTableInput").html(str)
                for(var i=0;i<data.data.length;i++){
                    str='   <tr id="'+data.data[i].uid+'">\n' +
                        '<td >'+data.data[i].uid+'</td>\n' +
                        '<td>'+data.data[i].name+'</td>\n' +
                        '<td>'+data.data[i].username+'</td>\n' +
                        '<td>'+data.data[i].password+'</td>\n' +
                        '<td>'+data.data[i].create_time+'</td>\n' +
                        '<td>'+data.data[i].modify_time+'</td>\n' +
                        '<td><a name="staffAccountUpdate" class="btn pill fill">修改</a><a name="staffAccountDelete" class="btn pill fill">离职</a> </td>\n' +
                        '</tr>\n'
                    $("#staffAccountTableInput").append(str);
                }
            },
            error:function () {
                alert("错误");
            }
        });
    }
    function refreshRoad(){
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/refreshRoad",
            success:function(data){
                var str=""
                $("#roadInfoTableInput").html(str)
                for(var i=0;i<data.data.length;i++){
                    str='   <tr id="'+data.data[i].ruid+'" name="'+data.data[i].rid+'">\n'
                    if(data.data[i].uid==0){
                        str+= '<td>未分配</td>\n'
                    }else {
                        str+= '<td>'+data.data[i].uid+'</td>\n'
                    }
                      str+=  '<td>'+data.data[i].road_number+'</td>\n' +
                        '<td><p style="margin-left: 30px;width: 300px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">\n' +
                            data.data[i].line+'</p></td>\n' +
                        '<td>'+data.data[i].create_time+'</td>\n' +
                        '<td>'+data.data[i].modify_time+'</td>\n' +
                        '<td>'+data.data[i].note+'</td>\n' +
                        '<td><a name="roadUpdate" class="btn pill fill">修改</a><a name="roadDelete" class="btn pill fill">删除线路</a><a name="assignDelete" class="btn pill fill">删除分配</a> </td>\n' +
                        '</tr>\n'
                    $("#roadInfoTableInput").append(str);
                }
            },
            error:function () {
                alert("错误");
            }
        });

        //获取分配司机的前两项
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/get_allRoad",
            success:function(data){
                var str=""
                $("#lineNumber").html(str)
                for(var i=0;i<data.data.length;i++){
                    str='<option value ="'+data.data[i].rid+'">'+data.data[i].road_number+'</option>'
                    $("#lineNumber").append(str);
                }
            },
            error:function () {
                alert("错误1");
            }
        });
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/get_allDriver",
            success:function(data){
                var str=""
                $("#driver").html(str)
                $("#LineDriver").html(str)
                for(var i=0;i<data.data.length;i++){
                    str='<option value ="'+data.data[i].uid+'">'+data.data[i].uid+'|'+data.data[i].name+'</option>'
                    $("#driver").append(str);
                    $("#LineDriver").append(str);
                }
            },
            error:function () {
                alert("错误2");
            }
        });
    }
    var updateLineUid=0
    $("#updateLineConfirm").on("click",function () {
        var rid=$("#rid").val()
        var no=$("#roadUnmber").val()
        var direction= $("#LineDirection").val()
        var roadUnmber=no+direction
        var line=$("#LineInput").val()
       // alert($("#assignNote").attr("disabled"))
        if($("#assignNote").attr("disabled")=="disabled"){
            //禁用状态
            $.ajax({
                type:"post",
                data:{rid:rid,roadUnmber:roadUnmber,line:line},
                url:"http://127.0.0.1:8080/update_roadInfo",
                success:function(data){
                    alert(data.msg)
                    init();
                    $(".box").attr("hidden",true)
                    $("#boxChangeLine").attr("hidden",false)
                },
                error:function (e) {
                    alert("错误");
                }
            });
        }else {
/*            $.ajax({
                type:"post",
                data:{rid:rid,roadUnmber:roadUnmber,line:line},
                url:"http://127.0.0.1:8080/update_roadInfo",
                success:function(data){
                    //alert(data.msg)*/
                    var uid=$("#LineDriver").val()
                    var ruid=$("#ruid").val()
                    var note=$("#assignNote").val()
                    $.ajax({
                        type:"post",
                        data:{ruid:ruid,updateLineUid:updateLineUid,uid:uid,note:note,rid:rid,roadUnmber:roadUnmber,line:line},
                        url:"http://127.0.0.1:8080/update_driverAssign",
                        success:function(data){
                            alert(data.msg)
                            if(data.status==0){
                                init();
                                $(".box").attr("hidden",true)
                                $("#boxChangeLine").attr("hidden",false)
                            }
                        },
                        error:function (e) {
                            alert("错误");
                        }
                    });
                /*},
                error:function (e) {
                    alert("错误");
                }
            });*/

        }
    })
    $("#updateLineCancel").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxChangeLine").attr("hidden",false)
    })
    $(document).on("click","a[name='roadUpdate']",function(){
        var ruid=$(this).parents('tr').attr('id');
        var rid=$(this).parents('tr').attr('name');
        var uid=$($(this).parents('tr').children('td').get(0)).html()
        var roadNumber=$($(this).parents('tr').children('td').get(1)).html()
        var line=$($($(this).parents('tr').children('td').get(2)).children("p")).html()
        var no=roadNumber.toString().substring(0,4)
        var direction=roadNumber.toString().substring(4,roadNumber.toString().length)
        //alert(no+"|"+direction)
        var note=$($(this).parents('tr').children('td').get(5)).html()
        $("#roadUnmber").val(no)
        if(direction=="(&gt;)"){
            $("#LineDirection").val("(>)")
        }else{
            $("#LineDirection").val("(<)")
        }
        $("#rid").val(rid)
        $("#LineInput").val(line.replaceAll("&gt;",">"))
        if(uid=="未分配"){
            $("#assignNote").attr("disabled", true)
            $("#LineDriver").attr("disabled", true)
        }else {
            $("#assignNote").attr("disabled", false)
            $("#LineDriver").attr("disabled", false)
            $("#assignNote").val(note)
            $("#LineDriver").val(uid)
            updateLineUid=$("#LineDriver").val()
        }
        $(".box").attr("hidden",true)
        $("#boxUpdateLine").attr("hidden",false)
        $("#ruid").val(ruid)
    })
    $(document).on("click","a[name='roadDelete']",function(){

        var rid=$(this).parents('tr').attr('name');

        var r = confirm("线路删除后相关司机分配将被删除，确定删除吗？");
        if (r == true) {
            $.ajax({
                type:"post",
                data:{rid:rid},
                url:"http://127.0.0.1:8080/delete_Road",
                success:function(data){
                    alert(data.msg)
                    init();
                },
                error:function (e) {
                    alert("错误");
                }
            });
        } else {
            alert("已取消")
        }

    })
    $(document).on("click","a[name='assignDelete']",function(){
        var uid=$($(this).parents('tr').children('td').get(0)).html()
        var rid=$(this).parents('tr').attr('name');
        if(uid=="未分配"){
            alert("该线路未分配任何司机故而无法删除分配！")
        }else {
            $.ajax({
                type:"post",
                data:{uid:uid,rid:rid},
                url:"http://127.0.0.1:8080/delete_assign",
                success:function(data){
                    alert(data.msg)
                    init();
                },
                error:function (e) {
                    alert("错误");
                }
            });
        }
    })
    function isNo(x) {
        var pattern =/[A-Z]\d{3}$/;
        return pattern.test(x);
    }
    $("#addRoadConfirm").on("click",function () {
        var no=$("#roadUnmberInput").val()
        var direction=$("#direction").val()
        var roadNumber=no+direction
        var line=$("#line").val()
        if(!isNo(no)){
        alert("请输入正确格式的线路号")
        }
        else if(no==""||direction==""||line==""){
            alert("请输入完整信息！")
        }else {
            $.ajax({
                type:"post",
                data:{roadNumber:roadNumber,line:line},
                url:"http://127.0.0.1:8080/add_line",
                success:function(data){
                    alert(data.msg)
                    init();
                },
                error:function (e) {
                    alert("错误"+e.data);
                }
            });
        }
    })
    $("#addDriverConfirm").on("click",function () {
        var rid=$("#lineNumber").val()
        var uid=$("#driver").val()
        var note=$("#driverNote").val()
        if(note=="")note="无"
        $.ajax({
            type:"post",
            data:{rid:rid,uid:uid,note:note},
            url:"http://127.0.0.1:8080/add_Driver",
            success:function(data){
                alert(data.msg)
                init();
            },

            error:function () {
                alert("错误");
            }
        });
    })
    $("#updateStaffAccountConfirm").on("click",function () {
        var uid=$("#uid").val()
        var username=$("#usernameUpdateInput").val();
        var password= $("#passwordUpdateInput").val()
        if(username==""||password==""){
            alert("请填写完整信息")
        }else {

            $.ajax({//验证username是否重复
                type:"post",
                data:{uesrname:username,
                },
                url:"http://127.0.0.1:8080/check_username",
                success:function(data){
                    //alert(data.msg)
                    if(data.status==1||data.data.username==$("#usernameNow").val()){
                        $.ajax({
                            type:"post",
                            data:{uid:uid,username:username,password:password},
                            url:"http://127.0.0.1:8080/update_staffAccount",
                            success:function(data){
                                alert(data.msg)
                                init();
                                $(".box").attr("hidden",true)
                                $("#boxEmployeeAccount").attr("hidden",false)
                            },

                            error:function () {
                                alert("错误");
                            }
                        });
                    }else {
                        alert("用户名已存在！")
                    }
                },

                error:function () {
                    alert("错误");
                }
            });
        }
    })
    $("#updateStaffAccountCancel").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxEmployeeAccount").attr("hidden",false)
    })
    $(document).on("click","a[name='staffAccountUpdate']",function(){
        $("#uid").val($(this).parents('tr').attr('id'));
        //alert("修改"+$(this).parents('tr').attr('id'))
        $("#usernameNow").val($($(this).parents('tr').children('td').get(2)).html())
        $("#usernameUpdateInput").val($($(this).parents('tr').children('td').get(2)).html());
        $("#passwordUpdateInput").val($($(this).parents('tr').children('td').get(3)).html());
        $(".box").attr("hidden",true)
        $("#updateStaffAccount").attr("hidden",false)
    })
    $(document).on("click","a[name='staffAccountDelete']",function(){
        var uid=$(this).parents('tr').attr('id')
        var judge = prompt("现在执行的操作为危险操作，请输入动态验证码：");
        if (judge.trim()) {
            $.ajax({
                type:"post",
                data:{code:judge},
                url:"http://127.0.0.1:8080/check_GoogleAuthenticatorForAjax",
                success:function(data){
                    //alert(data.msg)
                    if(data.status==0){
                        $.ajax({
                            type:"post",
                            data:{uid:uid},
                            url:"http://127.0.0.1:8080/delete_staff",
                            success:function(data){
                                //alert(data.msg)
                                if(data.status==0){
                                    alert("离职操作成功")
                                    init()
                                }else {
                                    alert("删除错误")
                                }
                            },

                            error:function () {
                                alert("错误");
                            }
                        });
                    }else {
                        alert("动态验证码错误！")
                    }

                },

                error:function () {
                    alert("错误");
                }
            });
        } else {
            alert("已取消")
        }
    })
    function  refreshStaffRP(){
        $.ajax({
            type:"post",
            url:"http://127.0.0.1:8080/refreshStaffRP",
            success:function(data){
                var str=""
                $("#staffRPTableInput").html(str)
                for(var i=0;i<data.data.length;i++){
                    str='<tr id="'+data.data[i].rpid+'">\n' +
                        '<td >'+data.data[i].uid+'</td>\n' +
                        '<td>'+data.data[i].name+'</td>\n' +
                        '<td>'+data.data[i].type+'</td>\n' +
                        '<td>'+data.data[i].detail+'</td>\n' +
                        '<td>'+data.data[i].time.substring(0,10)+'</td>\n' +
                        '<td><a name="staffRPUpdate" class="btn pill fill">修改</a><a name="staffRPDelete" class="btn pill fill">删除</a> </td>\n' +
                        '</tr>\n'
                    $("#staffRPTableInput").append(str);
                }
            },
            error:function () {
                alert("错误");
            }
        });
    }
    $(document).on("click","a[name='staffRPUpdate']",function(){
        $("#rpidUpdate").val($(this).parents('tr').attr('id'));
      //  alert("修改"+$(this).parents('tr').attr('id'))
        $("#staffId").html($($(this).parents('tr').children('td').get(0)).html())
        $("#type").val($($(this).parents('tr').children('td').get(2)).html());
        $("#detail").val($($(this).parents('tr').children('td').get(3)).html());
        $("#RPTime").val($($(this).parents('tr').children('td').get(4)).html());

        $(".box").attr("hidden",true)
        $("#boxUpdateRewardPunishmentInfo").attr("hidden",false)
    })
    $("#updateStaffRPConfirm").on("click",function () {
        var rpid=$("#rpidUpdate").val();
        var type= $("#type").val();
        var detail= $("#detail").val();
        var  RPTime= $("#RPTime").val();
        if(type==""||detail==""||RPTime==""){
            alert("请填写完整信息！")
        }else {
            $.ajax({
                type:"post",
                data:{rpid:rpid,type:type,detail:detail,RPTime:RPTime},
                url:"http://127.0.0.1:8080/update_staffRP",
                success:function(data){
                    alert(data.msg)
                    init()
                    $(".box").attr("hidden",true)
                    $("#boxRewardPunishmentInfo").attr("hidden",false)
                },

                error:function () {
                    alert("错误");
                }
            });
        }
    })
    $("#updateStaffRPCancel").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxRewardPunishmentInfo").attr("hidden",false)
    })
    $(document).on("click","a[name='staffRPDelete']",function(){
        var rpid=$(this).parents('tr').attr('id')
        $.ajax({
            type:"post",
            data:{rpid:rpid},
            url:"http://127.0.0.1:8080/delete_RPInfo",
            success:function(data){
                //alert(data.msg)
                if(data.status==0){
                    alert("删除成功")
                    init()
                }else {
                    alert("删除错误")
                }
            },

            error:function () {
                alert("错误");
            }
        });
    })
    $("#addStaffRPConfirm").on("click",function (){
        var uid=$("#uidInput").val()
        var type=$("#typeInput").val()
        var detail=$("#detailInput").val()
        var time=$("#RPTimeInput").val()
        if(uid==""||type==""||detail==""||time==""){
            alert("请输入完整信息！")
        }else {
            $.ajax({
                type:"post",
                data:{uid:uid},
                url:"http://127.0.0.1:8080/check_StaffID",
                success:function(data){
                    if(data.status==0){
                        $.ajax({
                            type:"post",
                            data:{uid:uid,type:type,detail:detail,time:time},
                            url:"http://127.0.0.1:8080/add_StaffRP",
                            success:function(data){
                                alert(data.msg)
                                init();
                            },

                            error:function () {
                                alert("错误");
                            }
                        });
                    }else {
                        alert("员工ID不存在！")
                    }
                },

                error:function () {
                    alert("错误");
                }
            });
        }

    })
    $("#personalInfo").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxPersonalInfo").attr("hidden",false)
    })
    $("#personnelMatter").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxPersonnelMatter").attr("hidden",false)
    })
    $("#rewardPunishmentInfo").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxRewardPunishmentInfo").attr("hidden",false)
    })
    $("#addRewardPunishmentInfo").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxAddRewardPunishmentInfo").attr("hidden",false)
    })
    $("#compassionateLeave").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxCompassionateLeave").attr("hidden",false)
    })
    $("#changeLine").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxChangeLine").attr("hidden",false)
    })
    $("#assignDriver").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxAssignDriver").attr("hidden",false)
    })
    $("#addLine").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxAddLine").attr("hidden",false)
    })
    $("#employeeAccount").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxEmployeeAccount").attr("hidden",false)
    })

    $("#addEmployeeAccount").on("click",function () {
        $(".box").attr("hidden",true)
        $("#boxAddEmployeeAccount").attr("hidden",false)
    })
    function formpost(url, postData) {
        var tempform = document.createElement("form");
        tempform.action = url;
        tempform.target = "_self";
        tempform.method = "post";
        tempform.style.display = "none";
        for (var x in postData) {
            var opt = document.createElement("textarea");
            opt.name = x;
            opt.value = postData[x];
            tempform.appendChild(opt);
        }
        document.body.appendChild(tempform);
        tempform.submit();
    }
})